package com.financial.business.balance.service.gy.impl;

import com.financial.business.balance.constants.FinancialPayBalanceEnumConstants;
import com.financial.business.balance.constants.asyn.FinancialPayAsyncEnumConstants;
import com.financial.business.balance.dao.asyn.FinancialPayAsyncDao;
import com.financial.business.balance.domain.asyn.FinancialPayAsyncDO;
import com.financial.business.balance.service.gy.FinancialGYBalancePayAsynService;
import com.financial.business.balance.service.gy.FinancialGYBalancePayService;
import com.financial.business.balance.utils.HttpClientUtils;
import com.financial.business.balance.utils.JsonUtils;
import com.financial.business.balance.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //够样余额异步回执业务处理接口
 * @Date 2019/12/6 17:06
 */
@Service
public class FinancialGYBalancePayAsynServiceImpl implements FinancialGYBalancePayAsynService {
    public static final Logger logger = LoggerFactory.getLogger(FinancialGYBalancePayAsynServiceImpl.class);

    @Autowired
    private FinancialPayAsyncDao financialPayAsyncDao;

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Autowired
    private FinancialGYBalancePayService financialGYBalancePayService;

    @Override
    public ApiResult payAsyn(Map<String, Object> paramMap) {
        logger.info("FinancialGYBalancePayAsynServiceImpl.payAsyn---接收够样余额支付异步回执参数map格式:" + paramMap.toString());
        String payAsynJson = JsonUtils.objectToJson(paramMap);
        String orderNum = "";
        /*初始化返回值值*/
        String result = FinancialPayAsyncEnumConstants.ENUM_SEND_FAIL;
        try {
            /*状态码---0---成功*/
            String balanceCode = paramMap.get("balanceCode").toString();
            /*状态描述*/
            String message = paramMap.get("message").toString();
            /*预支付单号*/
            String prePayNo = paramMap.get("prePayNo").toString();
            /*支付金额*/
            String payMoney = paramMap.get("payMoney").toString();
            /*会员编号*/
            String customerUuid = paramMap.get("customerUuid").toString();
            /*订单号*/
            orderNum = paramMap.get("orderNum").toString();
            /*平台编号*/
            String registBusNum = paramMap.get("registBusNum").toString();
            /*余额预支付异步信息通知优点平台回执记录*/
            FinancialPayAsyncDO financialPayAsyncDO = new FinancialPayAsyncDO();
            financialPayAsyncDO.setBusinessChannel(FinancialPayAsyncEnumConstants.ENUM_BUSINESSCHANNEL_GYBALANCE);
            financialPayAsyncDO.setGmtCreate(new Date());
            financialPayAsyncDO.setGmtModified(new Date());
            financialPayAsyncDO.setIsDelete(FinancialPayAsyncEnumConstants.ENUM_ISDELETE_UNDEL);
            financialPayAsyncDO.setOutTradeNo(orderNum);
            financialPayAsyncDO.setPlatformNo(registBusNum);
            financialPayAsyncDO.setTotalAmount(new BigDecimal(payMoney));
            financialPayAsyncDO.setReturnMessage(payAsynJson);
            if (balanceCode.equals(FinancialPayAsyncEnumConstants.ENUM_GYBALANCECALLBANK_FAIL)) {
                financialPayAsyncDO.setState(FinancialPayAsyncEnumConstants.ENUM_STATE_SUC);
            } else {
                financialPayAsyncDO.setState(FinancialPayAsyncEnumConstants.ENUM_STATE_FAIL);
            }
            int res = financialPayAsyncDao.save(financialPayAsyncDO);
            if (res > 0) {
                /*获取请求回调地址*/
                Map<String, Object> condMap = new HashMap<>();
                condMap.put("outTradeNo", orderNum);
                condMap.put("state", FinancialPayBalanceEnumConstants.ENUM_STATE_SUC);
                String resqContent = financialGYBalancePayService.query(condMap);
                logger.info("FinancialGYBalancePayAsynServiceImpl.payAsyn---够样余额异步回执查询该笔订单的请求内容为!" + resqContent);
                if (resqContent != null && resqContent != "") {
                    /*查询resq_content内容中的notify_url回调地址*/
                    Map<String, Object> resqContentMap = JsonUtils.jsonToMap(resqContent);
                    String notifyUrl = (String) resqContentMap.get("notify_url");
                    logger.info("FinancialGYBalancePayAsynServiceImpl.payAsyn---够样余额异步回执查询该笔订单的回执地址!" + notifyUrl);
                    /*通知调用中心*/
                    result = httpClientUtils.doPost(notifyUrl, paramMap);
                    logger.info("FinancialGYBalancePayAsynServiceImpl.payAsyn---够样余额发送成功!");
                    if (balanceCode.equals(FinancialPayAsyncEnumConstants.ENUM_GYBALANCECALLBANK_FAIL)) {
                        return ApiResult.success(result);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialGYBalancePayAsynServiceImpl.payAsyn---够样余额回调调用平台失败:" + e.getMessage());
        }
        return ApiResult.failure(result);
    }
}
